//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using NopSolutions.NopCommerce.BusinessLogic.Model;
using NopSolutions.NopCommerce.BusinessLogic.SEO;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Web.Modules;
using NopSolutions.NopCommerce.Controls;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class ProductDetailsControl : BaseNopAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                Product product = this.ProductService.GetProductById(this.ProductId);
                if (product != null) {
                    lblTitle.Text = Server.HtmlEncode(product.Name);
                }

                //this.SelectTab(this.ProductTabs, this.TabId);
            }
        }

        protected override void OnPreRender(EventArgs e) {
            var txtName = ctrlProductInfoEdit.FindControl("txtName") as SimpleTextBox;
            if (txtName != null) {
                txtProductCopyName.Text = "Copy of " + txtName.Text;
            }

            Product product = this.ProductService.GetProductById(this.ProductId);
            if (product != null) {
                PreviewButton.OnClientClick = string.Format("javascript:OpenWindow('{0}', 800, 600, true); return false;", SEOHelper.GetProductUrl(product.ProductId));
            }

            base.OnPreRender(e);
        }

        protected Product Save() {
            Product product = null;
            //uncomment this line to support transactions
            //using (var scope = new System.Transactions.TransactionScope())
            {
                product = ctrlProductInfoEdit.SaveInfo();
                ctrlProductSEO.SaveInfo();
                ctrlProductVariants.SaveInfo();
                ctrlProductCategory.SaveInfo();
                ctrlProductManufacturer.SaveInfo();
                ctrlRelatedProducts.SaveInfo();
                ctrlCrossSellProducts.SaveInfo();
                ctrlProductPictures.SaveInfo();
                ctrlProductSpecifications.SaveInfo();

                this.CustomerActivityService.InsertActivity(
                    "EditProduct",
                    GetLocaleResourceString("ActivityLog.EditProduct"),
                    product.Name);

                //uncomment this line to support transactions
                //scope.Complete();
            }

            return product;
        }

        protected void SaveButton_Click(object sender, EventArgs e) {
            if (Page.IsValid) {
                try {
                    Product product = Save();
                    //Response.Redirect("Products.aspx");
                } catch (Exception exc) {
                    ProcessException(exc);
                }
            }
        }

        //protected void SaveAndStayButton_Click(object sender, EventArgs e) {
        //    if (Page.IsValid) {
        //        try {

        //            Product product = Save();
        //            Response.Redirect(string.Format("ProductDetails.aspx?ProductID={0}", product.ProductId));
        //        } catch (Exception exc) {
        //            ProcessException(exc);
        //        }
        //    }
        //}

        protected void DeleteButton_Click(object sender, EventArgs e) {
            try {
                Product product = this.ProductService.GetProductById(this.ProductId);
                if (product != null) {
                    this.ProductService.MarkProductAsDeleted(this.ProductId);

                    this.CustomerActivityService.InsertActivity(
                        "DeleteProduct",
                        GetLocaleResourceString("ActivityLog.DeleteProduct"),
                        product.Name);

                }
                Response.Redirect("Products.aspx");
            } catch (Exception exc) {
                ProcessException(exc);
            }
        }

        protected void BtnDuplicate_OnClick(object sender, EventArgs e) {
            try {
                Product productCopy = this.ProductService.DuplicateProduct(ProductId, txtProductCopyName.Text, cbIsProductCopyPublished.Checked, cbCopyImages.Checked);
                if (productCopy != null) {
                    this.CustomerActivityService.InsertActivity(
                        "AddNewProduct",
                        GetLocaleResourceString("ActivityLog.AddNewProduct"),
                        productCopy.Name);

                    Response.Redirect(String.Format("ProductDetails.aspx?ProductID={0}", productCopy.ProductId));
                }
            } catch (Exception ex) {
                ProcessException(ex);
            }
        }

        public int ProductId {
            get {
                return CommonHelper.QueryStringInt("ProductId");
            }
        }

        protected string TabId {
            get {
                return CommonHelper.QueryString("TabId");
            }
        }
    }
}